package com.x.erp.util;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.x.framework.remote.HttpClient;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.io.IOException;
import java.io.StringReader;
import java.util.HashMap;
import java.util.Map;

@Component
public class FirstCarApi {

	private Logger logger = LoggerFactory.getLogger(FirstCarApi.class);

	@Autowired
	private HttpClient httpClient;

	/**
	 * 登录
	 * @param param
	 * @return
	 * @throws Exception
	 */
	public FirstCarResult login(String param) throws Exception{
		String res = httpClient.doPostXml(Constant.FIRST_URL, param);
		String str = parserXml(res);
		FirstCarResult firstCarResult = new FirstCarResult();
		JSONObject jsonObj = JSONObject.parseObject(str);
		//获取json对象中的json数组
		JSONArray arr = jsonObj.getJSONArray("shops");
		String shop_id = null;
		if (arr != null && arr.size() > 0){
			shop_id = arr.getJSONObject(0).getString("id");
		}
		String sid = jsonObj.getString("sid");
		String error = jsonObj.getString("error");
		String error_code = jsonObj.getString("error_code");
		String request = jsonObj.getString("request");
		firstCarResult.setSid(sid);
		firstCarResult.setShop_id(shop_id);
		firstCarResult.setError(error);
		firstCarResult.setError_code(error_code);
		firstCarResult.setRequest(request);
		return firstCarResult;
	}

	/**
	 * 车辆图片上传
	 * @param param
	 * @return
	 * @throws Exception
	 */
	public FirstCarResult uploadImage(String param) throws Exception{
		String res = httpClient.doPostXml(Constant.FIRST_URL, param);
		String str = parserXml(res);
		return JSONObject.parseObject(str, FirstCarResult.class);
	}

	/**
	 * 添加或修改车源信息
	 * @param param
	 * @return
	 * @throws Exception
	 */
	public FirstCarResult createOrUpdateCar(String param) throws Exception{
		logger.info("请求的xml：" + param);
		String res = httpClient.doPost(Constant.FIRST_URL, param,"text/xml;charset=utf-8");
		logger.info("返回的xml：" + res);
		String str = parserXml(res);
		return JSONObject.parseObject(str, FirstCarResult.class);
	}

	/**
	 * xml字符串转为 字符串
	 * @param strXML
	 * @return
	 */
	private String parserXml(String strXML) {
		if(StringUtils.isEmpty(strXML)){
			return null;
		}
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		try {
			DocumentBuilder builder = factory.newDocumentBuilder();
			StringReader sr = new StringReader(strXML);
			InputSource is = new InputSource(sr);
			Document doc = builder.parse(is);
			Element rootElement = doc.getDocumentElement();
			return rootElement.getFirstChild().getFirstChild().getTextContent();
		} catch (ParserConfigurationException e) {
			e.printStackTrace();
		} catch (SAXException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 获取颜色
	 * @return
	 */
	private Map<String, Integer> colorHash = new HashMap<String, Integer>();
	public FirstCarApi(){
		colorHash.put("银色",1);
		colorHash.put("黑色",2);
		colorHash.put("白色",3);
		colorHash.put("蓝色",4);
		colorHash.put("绿色",5);
		colorHash.put("灰色",6);
		colorHash.put("黄色",7);
		colorHash.put("红色",8);
		colorHash.put("金色",9);
		colorHash.put("橙色",10);
		colorHash.put("其他",11);
		colorHash.put("棕色",12);
		colorHash.put("紫色",13);
		colorHash.put("香槟色",11);
		colorHash.put("米色",11);
		colorHash.put("米灰",11);
		colorHash.put("米黄",11);
		colorHash.put("双色",11);
	}

	public Integer getColorId(String color) {
		return colorHash.get(color);
	}


}
